package com.mz.mattress.admin.interceptor;

import com.mz.mattress.admin.service.HongBaoService;
import com.mz.mattress.api.redis.RedisKeys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * 监听处理过期key
 */
@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private HongBaoService hongBaoService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        //获取失效的key
        String expiredKey = message.toString();

        log.info("redis_expired_key={}", expiredKey);

        if (expiredKey.startsWith(RedisKeys.CASHOUT_LOCK)) {

            String cashId = expiredKey.replaceAll(RedisKeys.CASHOUT_LOCK, "");
            hongBaoService.closeCashOutOrder(cashId);

        }
    }


}
